/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.android.gdata.project.client.activity;

import nu.zoom.android.gdata.project.client.Intents;
import nu.zoom.android.gdata.project.client.R;
import nu.zoom.android.gdata.project.client.StandardMenuHandler;
import nu.zoom.android.gdata.project.client.WKC;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class ManageProject extends Activity {
	private static final String LOG_TAG = WKC.LOG_TAG;
	private final StandardMenuHandler standardMenuHandler;

	public ManageProject() {
		this.standardMenuHandler = new StandardMenuHandler(this);
	}

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manageproject);
		final Intent intent = getIntent();
		final String action = intent.getAction();
		final int projectIndex = intent.getIntExtra(Intents.Extra.PROJECT_INDEX, -1);
		if (Intent.ACTION_EDIT.equalsIgnoreCase(action)) {
			final String projectName = intent.getStringExtra(Intents.Extra.PROJECT_NAME);

			Log.d(LOG_TAG, "Action Edit for project name: " + projectName + " with index: " + projectIndex);
			if (projectName != null) {
				final EditText text = (EditText) this.findViewById(R.id.EditProjectNameField);
				text.setText(projectName);
			}
		} else if (Intent.ACTION_INSERT.equalsIgnoreCase(action)) {
			Log.d(LOG_TAG, "Action Insert for project ");
		}
		View view = this.findViewById(R.id.ManageProjectSaveButton);
		view.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				final EditText text = (EditText) ManageProject.this.findViewById(R.id.EditProjectNameField);
				final String projectName = text.getText().toString();
				if ((projectName == null) || (projectName.length() < 1)) {
					Toast toast =
							Toast.makeText(ManageProject.this, R.string.error_projectname_empty, Toast.LENGTH_SHORT);
					toast.show();
				} else {
					Intent resultIntent = Intents.editProject(ManageProject.this, projectName, projectIndex);
					setResult(RESULT_OK, resultIntent);
					finish();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		standardMenuHandler.onCreateOptionsMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		return standardMenuHandler.onOptionsItemSelected(item);
	}

	@Override
	protected Dialog onCreateDialog(final int id) {
		return standardMenuHandler.onCreateDialog(id);
	}

}
